<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* This is new class model for development 
* Author    : Setyagus Sucipto
* Email     : mailbox@icreativelabs.com
* Website   : icreativelabs.com    
*/ 
class MY_Model extends Model{
    var $errorMessage = "";
    var $recordId = "";
    var $haveError = false;
    var $tableName = '';
    var $debug,$query;

    function My_Model(){
        parent::Model();
        $this->load->database();
        $this->debug = false;
    }    
/*
* call setdebug for print the sql that execute 
* $this->modelname->setdebug();
* param1 : boolean   
*/    
    function setDebug($status=true){
        $this->debug = $status;
    }   

/*
* set tablename for query default  
*/     

    function setTableName($tableName){
        $this->tableName = $tableName;
    }
/*
* will display message error on screen  
*/
    function setErrorMessage($errorMessage){
        global $__dbConn;
        if (trim($errorMessage) != ""){        
            $this->errorMessage .= "<li>".$errorMessage."</li>";
            $this->haveError = true;
        }
    }
/*
* check if there is error that set in function setErrorMessage 
*/    
    function isHaveError(){
        return $this->haveError;
    }
/*
* get Error message on system  
*/
    function getErrorMessage(){
        return "<ul>".$this->errorMessage."</ul>";
    }
/*
* will print the debug while setdebug active  
*/
    function printDebug(){
       if ($this->debug){ 
            echo "<br/><font color='red'><strong>Execute this query : ".$this->db->last_query()."</strong><br/></font>";
            $this->errorMessage = $this->db->call_function('error',$this->db->conn_id);  
            if ($this->errorMessage != ''){
                echo "<br/><font color='red'><strong>The Query Get Error : ".$this->errorMessage ."</strong><br/></font>";                         
            }
            else{
                echo "<br/><font color='red'><strong>The Query Success, rows affected : ".$this->getAffectedRows() ."</strong><br/></font>";
            }
       }   
    }
    
    function isEmpty($data,$errorMessage){
        if (trim($data) == ''){
            $this->setErrorMessage($errorMessage);
            return true;
        }
        return false;
    } 
    
    function insert($data){
        $this->db->insert($this->tableName, $data);
        $this->printDebug();
        return $this->db->insert_id();
    }

    function insertData($data){
        $this->db->insert($this->tableName, $data);
        $this->printDebug();
        return $this->db->insert_id();
    }
 
    function update($id, $data){
        $this->db->where('id', $id);
        $this->db->update($this->tableName, $data);
        $this->printDebug();
    }
    
    function delete($arrayData){
      $this->db->delete($this->tableName, $arrayData);           
    }
    
    function deleteById($id){
      $this->db->delete($this->tableName, array('id' => $id));   
    }
    
    function getAffectedRows(){
        return $this->db->call_function('affected_rows',$this->db->conn_id);
    }
    
    function execute($sql=''){
        $sql = trim($sql);
        if ($sql != ""){
          $this->sql = $sql;
        }
        if ($this->sql == ""){
          return false;
        }
        $this->db->trans_start();
        $this->query = $this->db->query($this->sql);        
        $this->db->trans_complete();
        $this->printDebug();
        if ($this->db->trans_status() === FALSE){
          $this->query  = false;
          return false;
        }
        else{
          return $this->query;
        }
    }
    
    function query($sql=''){
        return $this->execute($sql);       
    }
    
    function getDetail($id = '', $otherCondition = '', $orderBy = ''){
        $sqlData = "SELECT * FROM $this->tableName ";
        $detailCondition = "";
        if ($id != "") {
            $detailCondition = "WHERE id='$id' ";
        }
        if ($otherCondition != "") {
            if ($id != "") {
                $detailCondition .= " AND $otherCondition ";
            } else {
                $detailCondition .= "WHERE $otherCondition ";
            }
        }

        if (trim($orderBy != "")) {
            $detailCondition .= " ORDER BY $orderBy ";
        }
        if ($this->execute($sqlData.$detailCondition)){
            return $this->query->row_array();
        }
        return false;
    }
    
    function getList($page = "1", $maxList = "", $condition = '', $orderBy = '')
    {
        $condition = trim($condition);
        $maxList = trim($maxList);
        $orderBy = trim($orderBy);
        if ($condition != "") {
            $condition = " WHERE " . $condition;
        }
        if ($orderBy != "") {
            $orderBy = " ORDER BY " . $orderBy;
        }

        if ($maxList == "") {
            $sqlData = "SELECT * FROM $this->tableName $condition $orderBy ";
            $this->execute($sqlData);
        } else {
            if ($page == 0) {
                $page = 1;
            }
            $page = $page - 1;
            $startNumber = $page * $maxList;
            $startNumber < 0 ? $startNumber = 0 : $startNumber = $startNumber;

            $sqlData = "SELECT * FROM $this->tableName $condition $orderBy 
                        LIMIT $startNumber,$maxList";

            $this->execute($sqlData);
        }
        $resultData = $this->query->result_array();
        if (is_array($resultData)) {
            return $resultData;
        }
        return false;
    }
    
    
    
    function getListBySQL($sqlData, $page = "1", $maxList = ""){
        if ($maxList == "") {
            $this->execute($sqlData);
        } else {
            if ($page == 0) {
                $page = 1;
            }
            $page = $page - 1;
            $startNumber = $page * $maxList;
            $startNumber < 0 ? $startNumber = 0 : $startNumber = $startNumber;

            $sqlData .= " LIMIT $startNumber,$maxList";

            $this->execute($sqlData);
        }
        return $this->query->result_array();
    }
    
    function findById($id){
        if ($id == NULL){
            return NULL;
        }
 
        $this->db->where('id', $id);
        $query = $this->db->get($this->tableName);
 
        $result = $query->result_array();
        $this->printDebug();
        return (count($result) > 0 ? $result[0] : NULL);
    }
 
    function findAll($sort = 'id', $order = 'asc'){
        $this->db->order_by($sort, $order);
        $query = $this->db->get($this->tableName);
        $this->printDebug();
        return $query->result_array();
    }
}
  
?>
